<template>
	<view class="container" :style="'height:'+ winHeight+'px;'">
		<view class="login-content">
			企业微信登录中...
			<view style="display: none;">
				<view>
					code:{{qyweinxin.code}}
				</view>
				<view>
					state:{{qyweinxin.state}}
				</view>
				<view  @click="loginCertification">点击请求登录</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	let weixinAuthService
	export default {
		computed: mapState(['forcedLogin', "__serverpath"]),
		onLoad: function(option) {
			//option为object类型，会序列化页面Url传递的参数
			console.info(option);
		},
		created() {
			var that = this;
			that.$store.commit('logout')
			uni.getSystemInfo({
				success: function(res) {
					that.winHeight = res.windowHeight;
				}
			});
			// 微信第一次握手后得到的code 和state
			var _code = Object.toQueryString("code");
			var _state = Object.toQueryString("state");
			that.qyweinxin.code = _code;
			that.qyweinxin.state = _state;
			console.info("_code:" + _code);
			console.info("_state:" + _state);
			if (that.qyweinxin.code && that.qyweinxin.state) {
				that.loginCertification();
			}
			// uni.request({
			// 	url:  that.serverUrl+'/login/getLoginPageData',
			// 	success: function(res) {
			// 		console.info(res);
			// 		var resData = res.data;
			// 		if (resData.success) {
			// 			that.sys = resData.data;
			// 			var f2bpmSysConfig = that.sys;
			// 			that.$store.commit('f2bpmSysConfig', f2bpmSysConfig)
			// 		} else {
			// 			uni.showModal({
			// 				showCancel:false,
			// 				content: resData.msg,
			// 			});
			// 		}
			// 	},
			// 	fail: function(res) {
			// 		console.info(res);
			// 	}
			// });
		},
		data() {
			return {
				//企业微信参数
				qyweinxin: {
					code: "",
					state: "",
					corpId: "AppDefault"
				},
				positionTop: 0,
				providerList: [],
				loading: false,
				hasProvider: false,
				winHeight: 800,
				user: {
					tenantId: '',
					userName: '',
					password: '',
					//接入的系统应用ID
					corpId: "AppDefault"
				},
				sys: {}

			}
		},
		methods: {
			// 映射到state中的mutations中的loginauthor
			...mapMutations(['loginauthor']),
			loginCertification() {
				let that = this;
				try {
					//===使用自己的ajax======
					uni.showLoading({
						mask: true,
						title: "企业微信认证中..."
					});
					uni.request({
						url: that.serverUrl + '/login/weiXinLogin/certification',
						data: that.qyweinxin,
						method: 'POST',
						header: {
							'Content-type': 'application/x-www-form-urlencoded'
						},
						success: function(res) {
							console.info(res);
							var result = res.data;
							if (result.success) {
								let author = result.data;
								//author.realName=decodeURI(author.realName);
								that.$store.commit('loginauthor', author)
								that.loginLocal()
							} else {
								that.$store.commit('logout')
								uni.showModal({
									showCancel: false,
									content: result.msg,
								});
							}
						},
						complete: () => {
							uni.hideLoading();
						},
						fail: function(res) {
							if (res.errMsg.indexOf("ERR_CONNECTION_REFUSED")) {
								uni.showToast({
									icon: 'none',
									title: '网络错误，请求失败'
								});
							}
							uni.hideLoading();
						}
					});
				} catch (e) {
					uni.showModal({
						content: '请求异常' + e.toString()
					})
				}
			},
			loginLocal() {
				uni.setStorageSync('login_type', 'local')
				this.toMain();
			},
			toMain() {
				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../home/index',
					});
				} else {
					uni.navigateBack();
				}
			}
		}
	}
</script>

<style>
	@import url("@/static/css/login.css");
</style>
